import axios from "axios"
import { ElMessage } from 'element-plus'
const instance =axios.create({
    baseURL:"/api",
    timeout:5000
})

// 请求拦截器

instance.interceptors.request.use(
    (config)=>{

        const token = localStorage.getItem("token")
        if(token){
            if(config.headers){
                config.headers["Authorization"]=token
            }
        }
        return config
    },
    (eer)=>Promise.reject(eer)
)

// 响应拦截器

instance.interceptors.response.use(
    (response)=>{
        const data = response.data as IResponse<any>
        if(data.code!==200){
            ElMessage.error(data.message)
        }
        return response.data
    },
    (eer)=>{
    ElMessage.error("服务器错误，请联系管理员"+eer.message)
     Promise.reject(eer)   
    }
)

export default instance